package com.syaccp.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class imgUtils {
    public static final String DEFAULT_PIC = "http://127.0.0.1/images/defaultImage.jpg";
    public static final String PICTURE__PATH_PREFIX = "https://api.hntututu.com/fullView/";
    public static final String PICTURE__PATH_TWO_PREFIX = "https://image.hntututu.com/";
    public static final String PICTURE_CONCAT = "/panos/";
    public static final String PICTURE_PATH_SUFFIX = ".tiles/thumb.jpg";


    public static String setRealPicPath(String picturePath) {
        String PicturePath = "";
        //查询是否是有图片的路径，如果为没有图片的路径，返回默认路径
        if (picturePath.contains("_f.jpg")) {
            String keyword = picturePath.replace("_f.jpg", "");
            PicturePath = PICTURE__PATH_PREFIX + keyword + PICTURE_CONCAT + keyword + PICTURE_PATH_SUFFIX;
        } else if (!picturePath.contains("_f.jpg")) {
            String keyword = picturePath.replace(".jpg", "");
            PicturePath = PICTURE__PATH_PREFIX + keyword + PICTURE_CONCAT + keyword + PICTURE_PATH_SUFFIX;
        }
        return PicturePath;
    }
    public static String setRealPicPathTwo(String picturePath) {
        String PicturePath = "";
            PicturePath = PICTURE__PATH_TWO_PREFIX + picturePath;
        return PicturePath;
    }
    /**
     * 检查Web网页是否存在
     *
     * @param url 地址
     * @return true：网页存在  false：网页不存在
     */
    public static boolean checkUrlConnection(String url) {
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Content-Type", "application/json");
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(600)// 设置连接主机服务超时时间
                .setConnectionRequestTimeout(1000)// 设置连接请求超时时间
                .setSocketTimeout(1000)// 设置读取数据连接超时时间
                .build();
        // 添加配置
        httpGet.setConfig(requestConfig);
        // 设置请求头
        CloseableHttpClient httpclient;
        CloseableHttpResponse response;
        int statusCode;
        boolean flag = true;
        httpclient = HttpClients.createDefault();// 创建Httpclient对象
        try {
            // 执行请求
            response = httpclient.execute(httpGet);
            statusCode = response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            return false;
        }
        if (statusCode == 404) flag = false;
        return flag;
    }

}
